package com.bwie.oss;

import com.aliyun.oss.OSS;
import com.bwie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author 国兴越
 * @version 1.0
 * Create by
 */


@RestController
@RequestMapping("upload")
public class UploadController {



    @Autowired
    private OSS oss;
    /**
    * //TODO 文件上传
    * @author 国兴越
    * @date 2024/3/5 19:40
    */
    @PostMapping("/fileUpload")
    public String uploadFile(@RequestBody MultipartFile file) throws IOException {


        //处理文件名
        String filename = file.getOriginalFilename();
        //获取真实文件名
        String suffix = filename.substring(filename.lastIndexOf(".") + 1);

        //根据文件解析出文件扩展名
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        //随即生产文件名uuid
         String fileName=uuid+"."+suffix;
        //上传
            oss.putObject("2108-gxy",fileName,file.getInputStream());


       return "https://2108-gxy.oss-cn-beijing.aliyuncs.com/"+fileName;

        //返回路径


    }





}
